var express = require('express');
var router = express.Router();
const moment = require('moment');
//招聘list
router.get('/recruitment/list', function (req, res, next) {
    res.render('admin_tmpl/recruitment/list.html', {});
});

//招聘添加
router.get('/recruitment/add', async (req, res, next) => {
    res.render('admin_tmpl/recruitment/add.html', {});
});

//招聘修改
router.get('/recruitment/edit', async (req, res, next) => {
    var id = req.query.id;
    var SQL = 'select * from t_recruitment where delete_flag = 0 and id="' + id + '"';
    await common.prototype.operation(SQL).then(rest => {
        var data = rest[0];
        res.render('admin_tmpl/recruitment/edit.html', data);
    }, (error) => {
        console.log(error)
    }).catch((err) => {
        console.log(err)
    })

});

/*
 * 招聘列表
 * @return
 *
 */
router.post('/recruitment/getList', async (req, res, next) => {
    var condition = '';
    var SQL = '';
    var conditionSQL = '';
    var allData = '';
    var begin = 0;
    var data = req.body;
    var pageSize = data.pageSize;
    var pageNumber = data.pageNumber;
    var likeName = req.body.search_like_name;
    var likeType = req.body.search_like_type;
    if (likeName) {
        condition = condition + 'and position("' + likeName + '" IN name)'
    }
    if (likeType) {
        condition = condition + 'and position("' + likeType + '" IN type)'
    }

     SQL = 'select * from t_recruitment where delete_flag = 0 ' + condition + '';
    await common.prototype.operation(SQL).then(async rest => {
        if (pageSize) {
            begin = (pageNumber - 1) * pageSize;
            condition = condition + 'order by id desc limit ' + begin + ',' + pageSize + ''
        }
        allData = rest;
        conditionSQL = 'select * from t_recruitment where delete_flag = 0 ' + condition + ''
        await common.prototype.operation(conditionSQL).then(async result => {
            res.send({
                code: 0, msg: '操作成功',
                count: allData.length, data: result
            })
        })
    }, (error) => {
        console.log(error)
        res.send({
            code: 404, msg: '操作失败',
            data: error
        })
    }).catch((err) => {
        console.log(err)
    })
});

/*
 * 添加招聘
 * @return
 *
 */
router.post('/recruitment/addRecruitment', async (req, res, next) => {
    var data = req.body;
    var date = moment().format('YYYY-MM-DD HH:mm:ss');
    var content = JSON.parse(JSON.stringify("'"+data.content+"'"));
    var SQL = 'INSERT INTO `t_recruitment`(name,position_type,city,type,content,create_date,modify_date,delete_flag) VALUES("' + data.name + '","' + data.positionType + '","' + data.city + '","' + data.type + '","' + content + '","' + date + '","' + date + '",0) '
    await common.prototype.operation(SQL).then(rest => {
        res.send({
            type: 'success', content: '添加成功'
        })
    }, (error) => {
        console.log(error)
        res.send({
            type: 'error', content: '添加失败'
        })
    }).catch((err) => {
        console.log(err)
    })
});

/*
 * 删除招聘
 * @return
 *
 */
router.post('/recruitment/delete', async (req, res, next) => {
    var data = req.body;
    if (data.id) {
        var date = moment().format('YYYY-MM-DD HH:mm:ss');
        var SQL = 'update t_recruitment set delete_flag=1, modify_date="' + date + '" where id="' + data.id + '"'
        await common.prototype.operation(SQL).then(rest => {
            res.send({
                type: 'success', content: '删除成功'
            })
        }, (error) => {
            console.log(error)
            res.send({
                type: 'error', content: '删除失败'
            })
        }).catch((err) => {
            console.log(err)
        })
    } else {
        res.send({
            type: 'apiexception', content: '接口异常'
        })
    }
});

/*
 * 修改招聘信息
 * @return
 *
 */
router.post('/recruitment/update', async (req, res, next) => {
    var data = req.body;
    if (data.id) {
        var date = moment().format('YYYY-MM-DD HH:mm:ss');
        var content = JSON.parse(JSON.stringify("'"+data.content+"'"));
        var SQL = 'update t_recruitment set name="' + data.name + '",position_type="' + data.positionType + '",city="' + data.city + '",type="' + data.type + '",content="' + content + '",modify_date="' + date + '" where id="' + data.id + '"'
        await common.prototype.operation(SQL).then(rest => {
            res.send({
                type: 'success', content: '添加成功'
            })
        }, (error) => {
            console.log(error)
            res.send({
                type: 'error', content: '添加失败'
            })
        }).catch((err) => {
            console.log(err)
        })
    } else {
        res.send({
            type: 'apiexception', content: '接口异常'
        })
    }
});


module.exports = router;